package com.millstein.controller;


import com.millstein.request.LockProductRequest;
import com.millstein.service.ProductService;
import com.millstein.util.JsonData;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author millstein
 * @since 2021-09-16
 */
@RestController
@RequestMapping("/api/product/v1")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/page_product")
    @ApiOperation("分页查询商品信息")
    public JsonData pageProduct(@ApiParam("当前页") @RequestParam(value = "page", defaultValue = "1") int page,
                                @ApiParam("每页记录数") @RequestParam(value = "size", defaultValue = "10") int size) {
        return JsonData.buildSuccess(productService.pageProduct(page, size));
    }

    @GetMapping("/detail/{product_id}")
    @ApiOperation("根据id查询商品信息")
    public JsonData findDetail(@ApiParam("商品id") @PathVariable("product_id") Long id) {
        return JsonData.buildSuccess(productService.findDetail(id));
    }

    @PostMapping("/lock_product")
    @ApiOperation("rpc调用-锁定商品库存")
    public JsonData lockProduct(@ApiParam("锁定商品库存") @RequestBody LockProductRequest lockProductRequest) {
        productService.lockProduct(lockProductRequest);
        return JsonData.buildSuccess();
    }
}

